package com.igeek.compus.study.seller.controller;

import com.igeek.compus.study.seller.entity.Seller;
import com.igeek.compus.study.seller.service.SellerService;
import com.igeek.compus.study.system.Constants;
import com.igeek.compus.study.user.controller.UserController;
import com.igeek.compus.study.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;
    @Autowired
    UserController userController;

    @ResponseBody
    @RequestMapping("list.do")
    public Map getSellerList(Seller seller){
        Map<String,Object> result=new HashMap<>();
        try{
            List<Seller> sellerList = sellerService.getSellerList(seller);
            result.put("data", sellerList);
            result.put("code", 0);
            result.put("msg", "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "系统异常");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("deleteSellerById.do")
    public int deleteSellerById(Integer id){
         return sellerService.deleteSellerById(id);

    }


    @RequestMapping("toSellerEdit.do")
    public ModelAndView toSellerEdit(Integer id){
        Map<String ,Object> result=new HashMap<>();
        System.out.println(id);
        Seller seller=new Seller();
        seller.setId(id);
        List<Seller> sellerList1 = sellerService.getSellerList(seller);
        System.out.println(sellerList1);

        result.put("userId",sellerList1.get(0).getUserId());
        result.put("nickName",sellerList1.get(0).getNickName());
        result.put("phone",sellerList1.get(0).getPhone());

        return new ModelAndView("/seller/seller_edit",result);
    }

    @ResponseBody
    @RequestMapping("updateSeller.do")
    public int updateSeller(Seller seller){
        return  sellerService.updateSeller(seller);
    }

    /**
     * 将用户添加为卖家，首先判断该用户是否存在,若不存在，添加失败
     * ，其次判断卖家里表里是否已存在，若已存在，添加失败
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("addSeller.do")
        public Map<String, Object> addSeller(Integer userId){
        Map<String,Object> result=new HashMap<>();
            Integer id=userId;
            //通过用户id是否可以在用户表里拿到值
            User userById = userController.getUserById(id);
            System.out.println(userId);
            //通过用户id是否已在卖家表里存在
            Seller seller=new Seller();
            seller.setUserId(id);
        List<Seller> sellerList = sellerService.getSellerList(seller);
        if(userById==null || userById.getNickName()==null){
            System.out.println("表里不存在");
                result.put("code", 1);
                result.put("msg", "系统异常");
                return result;
            }else if(sellerList!=null && sellerList.size()>0){

            System.out.println("seller表里已存在");
            result.put("code", 1);
            result.put("msg", "系统异常");
            return result;
        }else{
            sellerService.addSeller(userId);

            result.put("code", 0);
            result.put("msg", "添加成功");
            return result;
        }

    }

    @ResponseBody
    @RequestMapping("deleteSellerList.do")
    public Map<String, Object> deleteSellerList(String sellerIdList){
        Map<String,Object> result=new HashMap<>();

        List<String> sellerIds = Arrays.asList(sellerIdList.substring(1, sellerIdList.length() - 1).split(","));

        // 2.执行业务
        try {
            if (sellerService.deleteSellerList(sellerIds) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;

    }

}
